<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-friendadd"></i>分类维护
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="class_id" width="50" label="序号" align="center"></el-table-column>
        <el-table-column prop="classify_name" label="分类名称" align="center"></el-table-column>
        <el-table-column label="创建时间" align="center">
          <template slot-scope="scope">{{ scope.row.add_time }}</template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination_div">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Totals"
          :page-count="pageTotal"
          :page-size="10"
          :current-page.sync="currPage"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
    <!-- 编辑弹出框 -->
    <el-dialog title="账号修改" :visible.sync="editVisible" width="40%" @close="cancledialog('form')">
      <el-form ref="form" :model="form" label-width="80px" label-position="left" :rules="rules">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model.trim="form.name"></el-input>
        </el-form-item>
        <el-form-item label="排序号" prop="sort">
          <el-input v-model.trim="form.sort"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancledialog('form')">取 消</el-button>
          <el-button type="primary" @click="saveEdit('form')">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteRow">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import * as api from "../../http/api.js";
export default {
  data() {
    var nameYZ = (rule, value, callback) => {
      ///^[A-Za-z0-9\u4e00-\u9fa5]+$/; 汉字格式，英文字母格式，数组。不能有符号
      const regGZ = /^[A-Za-z\u4e00-\u9fa5]+$/;
      if (!value) {
        return callback(new Error("分类名称不能为空"));
      }
      if (!regGZ.test(value)) {
        callback(new Error("请输入正确的分类名称"));
      } else {
        callback();
      }
    };

    var sortYZ = (rule, value, callback) => {
      const regGZ = /^[1-9][0-9]{0,3}$/;
      if (!value) {
        callback();
      }
      if (!regGZ.test(value)) {
        callback(new Error("请输入正确的序号"));
      } else {
        callback();
      }
    };

    return {
      //表格数据
      tableData: [],
      editVisible: false, //编辑框里面默认看不见
      innerVisible: false, //如果分类中有文字显示不能删除的弹出框
      //编辑弹出框中数据
      form: {},
      //编辑里表格中的判断规则
      rules: {
        name: [{ validator: nameYZ, trigger: "blur" }],
        sort: [{ validator: sortYZ, trigger: "blur" }]
      },
      delVisible: false, //点击删除弹出的信息框状态默认为不显示
      Token: sessionStorage.getItem("Token"),
      currPage: 1, //分页页码
      Totals: 1, //总条数 默认为1条，在获取接口列表时在传递过来，改变1
      pageTotal: 2, //总页数
      Class_id: 1, //用来连接编辑和删除的键
      ClassName: "",
      Classify_sort: 1
    };
  },
  methods: {
    getData() {
      this.$post(api.class_list, {
        token: this.Token,
        page: this.currPage
      }).then(res => {
        if (res.status == "0") {
          this.tableData = res.data.map((val, index, arr) => {
            let jsons = [];
            jsons.class_id = val.class_id;
            jsons.classify_name = val.classify_name;
            jsons.add_time = val.add_time;
            return jsons;
          });
          //分页数据
          this.Totals = res.totals;
          this.pageTotal = res.pageTotal;
          this.currPage = res.currPage;
        } else {
          console.log(res.msg);
        }
        if (res.status == "relogin") {
          this.$message.error(res.msg);
          this.$router.push("/login");
        }
      });
    },
    handleEdit(index, row) {
      //点击编辑
      this.Class_id = this.tableData[index].class_id;
      this.$post(api.class_info, {
        token: this.Token,
        class_id: this.Class_id
      })
        .then(res => {
          if (res.status == "0") {
            this.form = {
              name: res.data.classify_name,
              sort: res.data.classify_sort,
              id: res.data.class_id
            };
          } else {
            this.$message.error(res.msg);
          }
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });

      this.editVisible = true;
    },
    handleDelete(index, row) {
      //删除按钮
      this.Class_id = this.tableData[index].class_id;

      this.delVisible = true;
    },
    cancledialog(formRule) {
      //清除验证
      this.$refs[formRule].resetFields();
      this.editVisible = false;
    },
    // 保存编辑
    saveEdit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.ClassName = this.form.name;
          this.Classify_sort = this.form.sort;
          this.Class_id = this.form.id;

          this.$post(api.class_edit, {
            token: this.Token,
            class_id: this.Class_id,
            classify_name: this.ClassName,
            classify_sort: this.Classify_sort
          })
            .then(res => {
              if (res.status == "0") {
                this.$message.success(res.msg);
                this.getData();
              } else {
                this.$message.error(res.msg);
              }
              if (res.status == "relogin") {
                this.$message.error(res.msg);
                this.$router.push("/login");
              }
            })
            .catch(error => {
              console.log(error);
            });

          this.editVisible = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 确定删除
    deleteRow() {
      this.$post(api.class_delete, {
        token: this.Token,
        class_id: this.Class_id
      })
        .then(res => {
          if (res.status == "0") {
            this.$message.success("删除成功");
            this.getData();
          } else {
            this.$message.error(res.msg);
          }
          if (res.status == "relogin") {
            this.$message.error(res.msg);
            this.$router.push("/login");
          }
        })
        .catch(error => {
          console.log(error);
        });

      this.delVisible = false;
    },
    //点击分页
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getData();
    }
  },
  created() {
    this.getData();
  }
};
</script>
<style scoped>
.pagination_div {
  margin-top: 20px;
  text-align: right;
}
.child_div {
  text-align: center;
}
.child_div p {
  margin-bottom: 20px;
}
</style>
